package com.zj.study.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.study.entity.ExcelEntity;
import com.zj.study.entity.User;
import com.zj.study.excel.ExcelListener;
import com.zj.study.mapper.UserMapper;
import com.zj.study.pojo.FinalPageVO2;
import com.zj.study.pojo.FinalResultVO;
import com.zj.study.pojo.query.UserQuery;
import com.zj.study.pojo.vo.UserVO;
import com.zj.study.service.UserService;
import com.zj.study.utils.beancopier.BeanCopier;
import com.zj.study.utils.util.PageUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * @author 赵赳
 * @since 2021/9/17 15:00
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Override
  public List<User> queryUser(UserQuery query) {
    return super.list(Objects.isNull(query) ? null : query.toQuery());
  }

  @Override
  public FinalPageVO2<User> queryOneOrList() {
    return FinalPageVO2.ok(lambdaQuery().page(new Page<>(1, 5)));
  }

  @Override
  public FinalResultVO<List<User>> getPage(int pageNum, int pageSize) {
    return FinalResultVO.ok(generateUserList(pageNum, pageSize));
  }


  public FinalResultVO<List<UserVO>> getUser() {

    var userList = lambdaQuery().list();

    var collect = userList.stream().map(t -> BeanCopier.copy(t, UserVO.class))
        .collect(Collectors.toList());

    return FinalResultVO.ok(collect);

  }


  @Override
  public void testExcel() throws FileNotFoundException {
    var file = new File("C:\\Users\\18210\\Desktop\\03交流桩出货信息汇总表20220327(1)(1).xlsx");
    var fileInputStream = new FileInputStream(file);
    EasyExcel.read(fileInputStream, ExcelEntity.class, new ExcelListener()).sheet("广汽交流桩湖南工厂发货")
        .doRead();
  }

  private final static Map<String, Integer> COUNT_SIZE_HASH_MAP = new ConcurrentHashMap<>(18);
  private final static String COUNT_SIZE = "count_size";


  @Override
  public FinalResultVO<Integer> countSize() {
    var list = List.of("a", "a", "a", "b", "b");
    int countSize = 0;
    COUNT_SIZE_HASH_MAP.put(COUNT_SIZE,countSize);
    for (String s : list) {
      if ("a".equals(s)) {
        getACountSize();
      } else {
        countSize = COUNT_SIZE_HASH_MAP.get(COUNT_SIZE) + 2;
        COUNT_SIZE_HASH_MAP.put(COUNT_SIZE, countSize);
      }
    }
    return FinalResultVO.ok(COUNT_SIZE_HASH_MAP.get(COUNT_SIZE));
  }

  private void getACountSize() {
    var countSize = COUNT_SIZE_HASH_MAP.get(COUNT_SIZE);
    countSize = countSize + 1;
    COUNT_SIZE_HASH_MAP.put(COUNT_SIZE, countSize);
  }

  /***
   * 生产数据
   *
   * @param pageNum  页码
   * @param pageSize 每页多少条数据
   * @return 分页后的数据
   */
  private List<User> generateUserList(int pageNum, int pageSize) {
    var users = List.of(new User().setAge(18).setName("张三"), new User().setAge(28).setName("张三1"),
        new User().setAge(38).setName("张三2"), new User().setAge(38).setName("张三3"),
        new User().setAge(38).setName("张三4"));
    return PageUtil.startPage(users, pageNum, pageSize);
  }


}
